package ooday04;

public class Test {
    public static void main(String[] args) {

/*
        Master master=new Master();
       Dog dog=new Dog("小黑",4,"黑色");
     Chick chick=new Chick("小花",2,"花色");
      Fish fish=new Fish("小金",1,"金色");
      Animal animal=dog;
        master.feed(animal);//在传参的同时，系统自动做了向上造型
        Animal animal1=chick;//父类的引用指向派生类的对象
        master.feed(chick);
        Animal animal2=fish;
        master.feed(fish);

 */
//        Animal animal=new Animal();抽象类不能被实例化
        Animal[] animals=new Animal[5];//创建Animal数组
        animals[0]=new Dog("黑子",5,"黑色");//给引用类型数组元素赋值时，需要new个对象
        animals[1]=new Dog("大黄",4,"黄色");
        animals[2]=new Chick("草鸡",3,"黄色");
        animals[3]=new Chick("公鸡",2,"红色");
        animals[4]=new Fish("金金",2,"金色");
        for (int i=0;i<animals.length;i++){
            System.out.println(animals[i].name);//访问引用类型数组元素的属性或行为时，需要打点访问
            animals[i].eat();
            animals[i].drink();

            if (animals[i] instanceof Dog ){//实现强制类型转换的条件：1、引用所指向的对象就是该类型
                Dog d=(Dog) animals[i];//2、引用所指向的对象实现了该接口或继承了该类
                d.lookHome();

            }
            if (animals[i] instanceof Fish){
                Fish fish=(Fish) animals[i];
                fish.swim();

            }

            if (animals[i] instanceof Chick){
                Chick chicks=(Chick) animals[i];
                chicks.layEggs();
            }


            if (animals[i] instanceof Swim){
           Swim swim=(Swim) animals[i];
           swim.swim();
            }
        }







    }
}
